package mylanguagechat;

import java.util.*;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

import com.google.wave.api.*;
import com.google.wave.api.event.*;

import cwcFramework.CWCFAbstractRobot;


public class MyLanguageChatRobot extends CWCFAbstractRobot {
	
	private static final String gadgetURL = "http://cwcf-mylanguagechat.appspot.com/gadget/language.xml";
	
	public MyLanguageChatRobot() {
		super();
	}
	
	@Override
	protected String getRobotEMail() {
		return "cwcf-mylanguagechat@appspot.com";
	}
	
	@Override
	protected String getRobotName() {
		return "MyLanguageChat CWCF";
	}

	@Override
	protected String getRobotAvatarUrl() {
		return "http://cwcf-mylanguagechat.appspot.com/avatar.png";
	}

	@Override
	protected String getRobotProfilePageUrl() {
		return "http://cwc-framework.googlecode.com";
	}
	
	@Override
	protected String getOAuthKey(boolean usingWaveSandbox) {
		if (usingWaveSandbox){
			return "";
		}else{
			return "";
		}
	}

	@Override
	protected String getOAuthSecret(boolean usingWaveSandbox) {
		if (usingWaveSandbox){
			return "";
		}else{
			return "";
		}
	}

	@Override
	protected String getSecurityToken() {
		return "";
	}

	@Override
	protected String getVerificationToken() {
		return "";
	}
	
	@Override
	public void initializePrivateWaves(Wavelet bufferWave, ArrayList<Wavelet> privateWaves){
		for (Wavelet wave : privateWaves) {
			Gadget gadget = new Gadget(gadgetURL);
			wave.getRootBlip().append("Please select the language you want to use. \n");
			wave.getRootBlip().append(gadget);			
			wave.getRootBlip().append("You will only get messages form other persons once you have set the language. \n" +
					"Any other blip you write here will be translated and written in the other person's chat." );
		}
	}
	
	public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
		// get wave, get participants
		Wavelet bufferWave = event.getWavelet();
		bufferWave.reply("\nHi from the My Language Chat robot! \nYou should add this robot in a Wave with 2 or more participants. " +
				"After that, check your Wave Inbox and open the private wave the robot just added for you. You will find more information there about how to use the robot.");
		Participants participants = bufferWave.getParticipants();
		
		//create chatting waves for participants
		startNAryCommunication(participants, bufferWave);
	}

	@Override
	public void onBlipSubmitted(BlipSubmittedEvent event){	
		if (!event.getModifiedBy().equals(getRobotEMail())) {
			if (!event.getBlip().isRoot()){
				Wavelet localWave = event.getWavelet();
				List<String> otherParticipants = getAllOtherParticipants(localWave);
				String mylanguage = readLocalGadgetState(event.getWavelet(), gadgetURL, "mylanguage");
				for (String participant : otherParticipants){
					LOG.info("Participant email:" + participant);
					String otherlanguage = readRemoteGadgetState(getRemoteWaveId(participant,event.getWavelet()), getRemoteWaveletId(participant,event.getWavelet()), gadgetURL, "mylanguage");
					if (mylanguage == null) mylanguage = "";
					if (otherlanguage != null){
						if (!otherlanguage.equals("")){
							String message = event.getBlip().getContent().replaceAll("\\\n", "");
							Translate.setHttpReferrer("http://cwcf-mylanguagechat.appspot.com/");
							String translatedMessage = "";
							try {
								translatedMessage = Translate.execute(message, Language.fromString(mylanguage), Language.fromString(otherlanguage));
							} catch (Exception e) {
								LOG.severe(e.getMessage());
							}							
							writeRemoteBlip(event.getWavelet(), getRemoteWaveId(participant,event.getWavelet()), getRemoteWaveletId(participant,event.getWavelet()), "\n\n" + event.getModifiedBy() + " said: \n" + translatedMessage);
						}			
					}
				}
			}
		}
	}

	
}
